package com.bjfu.college.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bjfu.college.mapper.IntentionSchemeIntentionRecordMapper;
import com.bjfu.college.mapper.StudentInfoIntentionSchemeMapper;
import com.bjfu.college.mapper.UniversityIntentionSchemeMapper;
import com.bjfu.college.pojo.StudentInfoIntentionScheme;
import com.bjfu.college.pojo.UniversityIntentionScheme;
import com.bjfu.college.service.UniversityIntentionSchemeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
* @author 87290
* @description 针对表【university_intention_scheme】的数据库操作Service实现
* @createDate 2023-07-13 17:37:39
*/
@Service
public class UniversityIntentionSchemeServiceImpl extends ServiceImpl<UniversityIntentionSchemeMapper, UniversityIntentionScheme>
    implements UniversityIntentionSchemeService{

    @Autowired
    UniversityIntentionSchemeMapper universityIntentionSchemeMapper;

    @Autowired
    IntentionSchemeIntentionRecordMapper intentionSchemeIntentionRecordMapper;

    @Autowired
    StudentInfoIntentionSchemeMapper studentInfoIntentionSchemeMapper;

    /**
     * @Description: 根据现有方案数以及创建者姓名新增方案
     * @Author: Pzf
     * @Date:   2023/7/15 20:37
     * @Param:  [creator]
     * @Return: boolean
     */
    @Override
    @Transactional
    public int insertScheme(int studentId){
        int insertNum;
        QueryWrapper<StudentInfoIntentionScheme> studentInfoIntentionSchemeQueryWrapper = new QueryWrapper<>();
        studentInfoIntentionSchemeQueryWrapper.eq("student_id",studentId);
        List<StudentInfoIntentionScheme> universityIntentionSchemes = studentInfoIntentionSchemeMapper.selectList(studentInfoIntentionSchemeQueryWrapper);
        if (universityIntentionSchemes == null || universityIntentionSchemes.size() >= 3) return 0;

        // 新方案实例
        UniversityIntentionScheme universityIntentionScheme = new UniversityIntentionScheme();
        LocalDateTime now = LocalDateTime.now();
        universityIntentionScheme.setCreatedTime(now);
        universityIntentionScheme.setModifiedTime(now);

        try {
            insertNum = universityIntentionSchemeMapper.insert(universityIntentionScheme);
            if (insertNum == 0){
                throw new RuntimeException("生成新方案失败");
            }

        } catch (Exception e) {
            throw new RuntimeException("生成新方案失败" + e.toString());
        }

        return universityIntentionScheme.getId();
    }

    /**
     * @Description: 根据方案id删除university_intention_scheme表中的记录
     * @Author: Pzf
     * @Date:   2023/7/16 18:04
     * @Param:  [schemeId]
     * @Return: int
     */
    @Override
    @Transactional
    public int deleteScheme(int schemeId){
        int result;
        UpdateWrapper<UniversityIntentionScheme> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id",schemeId);
        try {
            result = universityIntentionSchemeMapper.delete(updateWrapper);
        } catch (Exception e) {
            throw new RuntimeException("该方案记录删除失败");
        }

        return result;
    }
}




